package warehouse;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SupplyBean {

	public static void addSupply(int supplierNo,int cargoNo,int supplyNum) throws SQLException{
		Connection connection = DatabaseBean.connect();
		//添加新订单
		//String sql = "INSERT INTO supply(supplierNo,cargoNo,supplyNum) VALUES(?,?,?);";
		String sql = String.format("SELECT * FROM supply WHERE supplierNo='%s' AND cargoNo='%s'", 
				supplierNo,cargoNo);
		Statement stmt = connection.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		if(!rs.next()) {
			//如果结果集为空，则插入新记录
			sql = String.format("INSERT INTO supply(supplierNo,cargoNo,supplyNum) VALUES('%s','%s','%s');",
					supplierNo,cargoNo,supplyNum);
			stmt.executeUpdate(sql);
		}
		else {
			//否则，将对应记录的供应量增加
			sql = String.format("UPDATE supply SET supplyNum=supplyNum+%d WHERE supplierNo='%s' AND cargoNo='%s'; ",
					supplyNum,supplierNo,cargoNo);
			stmt.executeUpdate(sql);
		}
		
		
		
		if(connection!=null) {
			connection.close();
		}
	}
	
}
